package com.shop.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.entity.PageResult;
import com.shop.entity.Result;
import com.shop.mapper.ProductMapper;
import com.shop.pojo.Product;
import com.shop.pojo.ProductExample;
import com.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

//    /** 1.发布商品 */
//    @Override
//    public Result addProduct(Product pro) {
//        Result result = null;
//        if(pro == null || StringUtils.isEmpty(pro.getName())||
//                StringUtils.isEmpty(pro.getImage())||
//                pro.getNumber()==null|| pro.getNumber()<=0||
//                StringUtils.isEmpty(pro.getBesc())||
//                pro.getPrice()==null||pro.getPrice()<=0||
//                pro.getCategory()==null){
//            result = new Result(false,"请填写完整信息");
//        }else {
//            pro.setId(UUID.randomUUID().toString());
//            pro.setIsHot(0);
//            pro.setStatus(1);
//            pro.setImage("http://120.79.240.142:8080/images/"+pro.getImage());
//            pro.setCreateTime(new Date());
//            pro.setUpdateTime(new Date());
//            productMapper.insertSelective(pro);
//            result = new Result(true,"添加成功",pro);
//        }
//        return result;
//    }

    /** 2.查询指定商品详情 （根据书的id） */
    @Override
    public Product findProductById(String id) {
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    }

    /** 3.分页显示商品（类别查询） */
    @Override
    public PageResult findProductPage(Product pro, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        ProductExample tbe = null;
        if (pro != null) {
            tbe = new ProductExample();
            ProductExample.Criteria criteria = tbe.createCriteria();

            if (pro.getCategory()!=null) {
                criteria.andCategoryEqualTo(pro.getCategory()).andNumberGreaterThan(0).andStatusEqualTo(1);
            }
            // 此处可以扩展
        }

        Page<Product> page = (Page<Product>) productMapper.selectByExample(tbe);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /** 4.分页显示（模糊查询） */
    @Override
    public PageResult findLikeProductPage(Product pro, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        ProductExample tbe = null;
        if (pro != null) {
            tbe = new ProductExample();
            ProductExample.Criteria criteria = tbe.createCriteria();

            if (pro.getName()!=null) {
                criteria.andNameLike("%"+pro.getName()+"%").andStatusEqualTo(1).andNumberGreaterThan(0);
            }
            // 此处可以扩展
        }

        Page<Product> page = (Page<Product>) productMapper.selectByExample(tbe);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /** 5.查询热门商品（用于首页展示，后台定死查询数量，不需要分页） */
    @Override
    public List<Product> findHotProducts() {
        List<Product> products = productMapper.selectHottestProducts();
        return products;
    }

    /** 6.查询最新商品（用于首页展示，后台定死查询数量，不需要分页） */
    @Override
    public List<Product> findLatestProducts() {
        List<Product> products = productMapper.selectLatestProducts();
        return products;
    }

    /** 7.购买商品（修改商品的数量，商品数减number） */
    @Override
    public Result updateProduct(String pid, Integer number) {
        Result result = null;
        Product product = productMapper.selectByPrimaryKey(pid);
        //根据id获得商品后判断购买数量是否超出范围
        if(product!=null&&!StringUtils.isEmpty(product.getId())){
            //1.库存不足  购买失败
            if(product.getNumber()<number){
                result = new Result(false,"库存不足，购买失败");
                return result;
            }
            //2.刚好购买完
            if(product.getNumber()==number){
                product.setNumber(0);
                product.setStatus(0);
                product.setUpdateTime(new Date());
                productMapper.updateByPrimaryKeySelective(product);
                result = new Result(true,"购买成功",product);
            }if(product.getNumber()>number){
                product.setNumber(product.getNumber()-number);
                product.setUpdateTime(new Date());
                productMapper.updateByPrimaryKeySelective(product);
                result = new Result(true,"购买成功",product);
            }
        }
        return result;
    }

    /** 8查询商品的数量（用于前台的校验,可实现最大购买量不超过库存。。。。待定） */
    @Override
    public Integer findNumberById(String pid) {
        Product product = productMapper.selectByPrimaryKey(pid);
        if (product!=null&&!StringUtils.isEmpty(product.getId())){
            return product.getNumber();
        }
        return null;
    }
}
